package googleURLAnalyzer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import uk.co.mccann.gsb.engine.GoogleSafeBrowser;
import uk.co.mccann.gsb.exceptions.GSBException;
import uk.co.mccann.gsb.interfaces.GSBDAO;
import uk.co.mccann.gsb.interfaces.GSBEngine;
import uk.co.mccann.gsb.model.FileDAO;
import Utility.*;

public class GoogleURLAnalyzer {
	static GSBEngine gsb;
	static GSBDAO fileDao;
	static int MAX_URL_BATCH = 100;
	GoogleURLAnalyzer()
	{
		try
		{
			gsb = new GoogleSafeBrowser("ABQIAAAAlESu76xcrubTKKgj_L_r7xTtAO9UKrRyqIhi_dnWJXj1pg7bPg", "F:\\Programs\\java2\\twitterProject\\gsb\\", false);
			 
			 /* Use a flat file to read /store data (will create the files in the same directory as your preferences set in the session)*/
			fileDao = new FileDAO(); 
			 
			 /* Use MySQL Data Access Object to read / store data */
			//fileDao = new MySQLDAO("username", "password", "localhost", "your_db_name");
			 
			 /* Register MySQL DAO with GoogleSafeBrowser */
			gsb.registerDAO(fileDao);
		}
		catch(GSBException gsbe)
		{
			Utility.logMessage(gsbe.getMessage(), "GoogleURLAnalyzer_error.txt");
			System.exit(1);
		}
	}
	public static void update_blacklists()
	{
		try
		{
			/* Update blacklist and malwarelist (if you have never run this before, all data will be downloaded) */
			gsb.updateMalwarelist();
			gsb.updateBlacklist();
		}
		catch(GSBException gsbe)
		{
			Utility.logMessage(gsbe.getMessage(), "GoogleURLAnalyzer_error.txt");
		}
	}

	public void analyzeURLsFromFile(String inputFilename, String outputFilename)
	{
		String inputLine = "";
		String lineContents[] = null;
		int tweet_table_id = 0;
		String url = "";
		String screenname = "";
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter(outputFilename, true));
			BufferedReader input =  new BufferedReader(new FileReader(inputFilename));
			try {
				while (( inputLine = input.readLine()) != null){
					lineContents = inputLine.split("\\|");
					tweet_table_id = Integer.valueOf(lineContents[0]);
					url = lineContents[1];
					try
					{
						if(gsb.isDangerous(url)) 
						{
							System.out.println("Screenname: " + screenname + " from tweet: " + tweet_table_id);
							screenname = Utility.getScreennameFromTweetID(tweet_table_id);
							out.write(screenname+ " " + tweet_table_id + " " + url + "\n");
						}
					}
					catch (GSBException e) 
					{
						//e.printStackTrace();
						Utility.logMessage(e.getMessage() + " for " + url, "GoogleURLAnalyzer_error.txt");			
					}	
					//System.out.println("Screenname: " + screenname + " from tweet: " + tweet_table_id);
				}
			}
			finally {
				input.close();
			}
			out.close();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String args[])
	{
		GoogleURLAnalyzer gua = new GoogleURLAnalyzer();
		gua.analyzeURLsFromFile("filtered_urls_full.txt", "google_screennames.txt");
	}
}
